package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.OptionEntity;
import com.tfjybj.itoo.exam.model.OptionModel;
import com.tfjybj.itoo.exam.provider.service.OptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * OptionController
 * option表
 *
 * @author 李青松
 * @version 2.0.0
 * @since 2.0.0 2018-11-01 08:55:53
 */
@Api(tags = {"option表接口"})
@RequestMapping(value = "/option")
@RestController
public class OptionController {

    @Resource
    private OptionService optionService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model OptionModel
     * @return 添加的结果
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody OptionModel model) {
        if (StringUtils.isEmpty(model.getQuestionId())) {
            return ItooResult.build(ItooResult.FAIL, "questionId为空");
        }
        if (StringUtils.isEmpty(model.getOptionNameHtml())) {
            return ItooResult.build(ItooResult.FAIL, "optionNameHtml为空");
        }
        OptionEntity optionEntity = new OptionEntity();
        BeanUtils.copyProperties(model, optionEntity);
        optionService.save(optionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        optionService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        optionService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model OptionModel
     * @return 修改后的结果
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id修改option")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody OptionModel model) {
        if (StringUtils.isEmpty(model.getQuestionId())) {
            return ItooResult.build(ItooResult.FAIL, "questionId为空");
        }
        if (StringUtils.isEmpty(model.getOptionNameHtml())) {
            return ItooResult.build(ItooResult.FAIL, "optionNameHtml为空");
        }
        OptionEntity optionEntity = new OptionEntity();
        BeanUtils.copyProperties(model, optionEntity);
        optionService.updateById(optionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Option
     * @return ItooResult
     * @param id 主键id
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        OptionEntity optionEntity = optionService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", optionEntity);
    }

    /**
     * @return 分页查询所有Option
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "分页查询所有Option")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<OptionEntity> options = optionService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", options);
    }

    /**
     * 根据历史题id模糊查询option
     *
     * @param historyId 历史题id
     * @param pageNo    页码
     * @param pageSize  页数
     * @return 模糊查询的option
     * @author 李青松
     * @since 2.0.0 2018-11-01 08:55:53
     */
    @ApiOperation(value = "根据历史题id模糊查询option", notes = "分页根据历史题id模糊查询option")
    @GetMapping(value = "queryOptionByLikeHistoryId/{pageNo}/{pageSize}")
    public ItooResult queryOptionByLikeHistoryId(@RequestParam(required = false, defaultValue = "") String historyId,
                                                 @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                 @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<OptionEntity> optionList = optionService.queryByLikeHistoryId(historyId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", optionList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */


    /**
     * 根据quesitonId查找Option
     * @return ItooResult
     * @param questionId 试题id
     * @author 郭凯
     * @since 2.0.0 2018-11-01 08:55:53
     */

    @ApiOperation(value = "根据id查询选项实体")
    @GetMapping(value = {"/queryById/{questionId}"})
    public ItooResult queryById(@ApiParam(value = "questionId",required = true)
                                       @PathVariable String questionId){
        List<OptionModel> optionModelList = optionService.queryById(questionId);
        return ItooResult.build(ItooResult.SUCCESS,"查询成功",optionModelList);
    }





}    
